﻿using ExcelSheetOperateion;
using PrjAssessModels;
using PrjAssessModels.BaseModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace PrjAssess.Models.Service.AssessProject
{
    public class AssessProject
    {
        /// <summary>
        /// 从Excel中读取测算项目的信息
        /// </summary>
        /// <param name="file">Form中获取的上传的文件</param>
        /// <returns>测算结果</returns>
        public static Project ReadUpload(HttpPostedFileBase file)
        {
            try
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(HttpContext.Current.Request.MapPath("~/App_Data/Uploads/"), fileName);
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
                file.SaveAs(path);
                ReadAssessSheet read_sheet = new ReadAssessSheet();
                Project prj_infor = read_sheet.ReadWorkBook(path);
                return prj_infor;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 储存一个项目的测算信息,如果不存在:直接储存项目测算结果,返回true;如果存在:返回false,并输出msg的提示信息.
        /// </summary>
        /// <param name="prj">读取好的测算信息</param>
        /// <param name="need">是否需要重算</param>
        /// <param name="msg">输出的提示消息 </param>
        /// <returns>指示是否完成了储存操作</returns>
        public static bool CanDirectlySaveProject(Project prj,out bool need, out string msg)
        {
            need = false;
            msg = "";
            if (ProjectDataHelper.IsPrjExist(prj.PrjId))
            {
                need = ReAssessLimit.NeedReAssess(prj, out msg);
                return false;
            }
            else
            {
                ProjectDataHelper.InsertProject(prj);
                return true;
            }
        }







    }
}